<html>
<head>
  <title>tensorflow：操作（ops）</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="643"/>
<h1>tensorflow：操作（ops）</h1>

<div>
<span><div><div>tensorflow 中有的操作</div><div>操作： 直接可以用sess.run()的名字</div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 251px;"></col><col style="width: 295px;"></col><col style="width: 262px;"></col></colgroup><tbody><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>操作的名字</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>使用范例</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div>需要提供的变量（或者条件）</div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.constrant()</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>b = tf.constant(3)</div><div>sess.run(b)</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;">null</td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.add(a,b)</div><div>tf.multiply(a,b)</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>mul = tf.multiply(a, b)</div><div>sess.run(mul, feed_dict={a: 2, b: 3})</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div>a = tf.placeholder(tf.int16)</div><div>b = tf.placeholder(tf.int16)</div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.matmul</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>product = tf.matmul(matrix1, matrix2)</div><div>with tf.Session() as sess:</div><div>    result = sess.run(product)</div><div>    print result</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div>matrix1 = tf.constant([[3., 3.]])</div><div>matrix2 = tf.constant([[2.],[2.]])</div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.pow()</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>tf.pow(pred-Y, 2)</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div>Y = tf.placeholder(&quot;float&quot;)</div><div>pred = tf.add(tf.multiply(X, W), b)</div><div><br/></div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.reduce_sum(input_tensor,</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>axis=None,</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>keep_dims=False,</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>name=None,</div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div>reduction_indices=None)</div></blockquote></blockquote><div><br/></div><div>在维度上的操作</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div>tf.pow(pred-Y, 2)</div><div>n_samples = train_X.shape[0]</div><div>input_tensor: input tensor</div><div>axis:  the dimension you take sum operation</div><div>keep_dims: 表示是否保留原始数据的维度，False相当于执行完后原始数据就会少一个维度</div><div>reduction_indices:为了跟旧版本的兼容，现在已经不使用.</div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.train.GradientDesscentOptimizer().minimize()</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div>learning_rate = 0.01</div><div>cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)</div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.nn.softmax()</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>pred = tf.nn.softmax(tf.matmul(x, W) + b)</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div><br/></div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.global_variables_initializer()</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>init = tf.global_variables_initializer()</div><div>with tf.Session() as sess:</div><div>    sess.run(init)</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div><br/></div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.negative()</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>tf.negative(xte)</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div>xtr = tf.placeholder(&quot;float&quot;, [None, 784])</div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.abs()</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>tf.abs(tf.add(xtr, tf.negative(xte)))</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div><br/></div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div>tf.argmax()：用法tf.argmax(a,dimension),返回的是a中的某个维度最大值的索引，</div><div>tf.argmain()：用法tf.argmin(a,dimension),返回的是a中的某个维度最小值的索引，</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div>pred = tf.arg_min(distance, 0)</div><div>nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})</div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div><br/></div></td></tr><tr><td style="border: 1px solid rgb(204, 204, 204); width: 251px; padding: 8px;"><div><br/></div></td><td style="border: 1px solid rgb(204, 204, 204); width: 295px; padding: 8px;"><div><br/></div></td><td style="border: 1px solid rgb(204, 204, 204); width: 262px; padding: 8px;"><div><br/></div></td></tr></tbody></table><div><br/></div></div><div><br/></div><div><br/></div><div><br/></div><div>固有模型：</div><div><br/></div><div>random forest：</div><div>参数：</div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># Random Forest Parametershparams = tensor_forest.ForestHParams(num_classes=num_classes,</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">                                           num_features=num_features,</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">                                            num_trees=num_trees,</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">                                           max_nodes=max_nodes).fill()</span></div></div><div>使用：</div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">from tensorflow.contrib.tensor_forest.python import tensor_forest</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><br/></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">num_classes = 10 # The 10 digits</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">num_features = 784 # Each image is 28x28 pixels</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">num_trees = 10</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">max_nodes = 1000</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># Random Forest Parameters</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">hparams = tensor_forest.ForestHParams(num_classes=num_classes,</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>   <span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>num_features=num_features,</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>num_trees=num_trees,</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    <span>    </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>max_nodes=max_nodes).fill()</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># Build the Random Forest</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">forest_graph = tensor_forest.RandomForestGraphs(hparams)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># Get training graph and loss</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">train_op = forest_graph.training_graph(X, Y)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">loss_op = forest_graph.training_loss(X, Y)</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"><br/></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);"># Measure the accuracy</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">infer_op = forest_graph.inference_graph(X)</span></div></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></span>
</div></body></html> 